package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.warehouse.manage.dao.RoleMenuMapper;
import com.zhijian.warehouse.manage.entity.po.RoleMenu;
import com.zhijian.warehouse.manage.entity.vo.request.RoleMenuSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.RoleMenuVo;
import com.zhijian.warehouse.manage.service.RoleMenuService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 系统角色与菜单关联信息业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-01-22 15:37:49
 */
@Slf4j
@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper mapper;

    @Override
    public RoleMenu selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoleMenuVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(RoleMenu roleMenu) {
        Date now = new Date();
        roleMenu.setCreateTime(now);
        roleMenu.setUpdateTime(now);
        return mapper.insert(roleMenu) > 0 ? roleMenu.getId() : 0L;
    }

    @Override
    public boolean update(RoleMenu roleMenu) {
        Date now = new Date();
        roleMenu.setUpdateTime(now);
        return mapper.updateByPrimaryKey(roleMenu) != 0;
    }

    @Override
    public Integer count(RoleMenuSearchVo roleMenuSearchVo) {
        return mapper.count(roleMenuSearchVo);
    }

    @Override
    public List<RoleMenuVo> selectList(RoleMenuSearchVo roleMenuSearchVo) {
        return mapper.selectList(roleMenuSearchVo);
    }

    @Override
    public Page<RoleMenuVo> selectPage(RoleMenuSearchVo roleMenuSearchVo) {
        //先直接查总数
        int count = count(roleMenuSearchVo);
        //分页
        Page<RoleMenuVo> page = new Page<>(true, count, roleMenuSearchVo.getPageNum(), roleMenuSearchVo.getPageSize());
        roleMenuSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoleMenuVo> responseVos = this.selectList(roleMenuSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        mapper.deleteByRoleId(roleId);
    }

    @Override
    public void deleteByMenuId(Long menuId) {
        mapper.deleteByMenuId(menuId);
    }

    @Override
    public List<Long> selectMenuIdsByRoleId(Long roleId) {
        return mapper.selectMenuIdsByRoleId(roleId);
    }

    @Override
    public List<String> selectMenuNamesByRoleId(Long roleId) {
        return mapper.selectMenuNamesByRoleId(roleId);
    }

    @Override
    public List<String> selectEnMenuNamesByRoleId(Long roleId) {
        return mapper.selectEnMenuNamesByRoleId(roleId);
    }

    @Override
    public boolean insertBatch(List<RoleMenu> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}